/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.cfg;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.jdom.Element;

/**
 *
 */
public class PropertiesEl
{
	private final Properties properties = new Properties();

	PropertiesEl()
	{
		;
	}

	/**
	 * @return the properties
	 */
	public Properties getProperties()
	{
		return properties;
	}

	public PropertiesEl(Element el)
	{
		if(el == null) return;

		for(Element propertyEl : (List<Element>) el.getChildren("property"))
		{
			String name = propertyEl.getAttributeValue("name");
			name = name != null ? name : "";

			String value = propertyEl.getAttributeValue("value");
			value = value != null ? value : "";

			properties.setProperty(
					(name = name != null ? name : ""),
					(value = value != null ? value : ""));
		}
	}

	@Override
	public String toString()
	{
		if(properties.isEmpty()) return "";

		String val = "<properties>" + "\n";

		for(Iterator<Object> it = properties.keySet().iterator(); it.hasNext(); )
		{
			String k = (String) it.next();
			String v = properties.getProperty(k);

			val = val + "<property name=\"" + k + "\" value=\"" + v + "\" />" + "\n";
		}

		val = val + "</properties>";

		return val;
	}
}
